package com.fary.context.expression;

import com.fary.beans.factory.BeanFactory;
import com.fary.core.SpringException;
import com.fary.expression.BeanResolver;
import com.fary.expression.EvaluationContext;
import com.fary.util.Assert;

import java.rmi.AccessException;

public class BeanFactoryResolver implements BeanResolver {

    private final BeanFactory beanFactory;


    /**
     * Create a new {@link BeanFactoryResolver} for the given factory.
     *
     * @param beanFactory the {@link BeanFactory} to resolve bean names against
     */
    public BeanFactoryResolver(BeanFactory beanFactory) {
        Assert.notNull(beanFactory, "BeanFactory must not be null");
        this.beanFactory = beanFactory;
    }


    @Override
    public Object resolve(EvaluationContext context, String beanName) throws AccessException {
        try {
            return this.beanFactory.getBean(beanName);
        } catch (SpringException ex) {
            throw new AccessException("Could not resolve bean reference against BeanFactory", ex);
        }
    }

}